layui.config({
    base: '../../static/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index' //主入口模块
}).use(['form','layer'], function() {
    var form=layui.form;
    var layer=layui.layer;
    var id=urlQuery("id");

    if(id==null||id==''){
        id = null;
        //添加
    }else {
        $.ajax({
            url: "/admin/role/getInfo",
            data: {id: id},
            type: "get",
            success: function (result) {
                var role = result.data;
                if (result.code == 0){
                    $('#id').val(role.id);//主键id
                    $('#name').val(role.name);//角色名称
                    $('#remark').val(role.remark);//角色描述
                }

            },
            error: function () {
                alert("请求失败")
            }
        })
    }


    //监听提交
    form.on('submit(demo1)', function(data){
        var role= data.field;
        if(role.name==null||role.name==""){
            layer.msg("角色名称不能为空");
            return false;
        }
        $.ajax({
            url:"/admin/role/addAndUpdate",
            type:"post",
            data:role,
            success:function (result) {
                if (result.code==0){
                    layer.msg(result.message, {icon: 1,time:2000,shade:0.2});
                    setInterval(function () {
                        //当你在iframe页面关闭自身时
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        window.parent.location.reload();
                    },2000)
                }else {
                    layer.msg(result.message, {icon: 2,time:2000,shade:0.2});
                }
            }
        });
        return false;
    });
});